﻿#define MUTEX
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;



namespace CLVisionIOCardNC620.IOCardDevice
{
    public class IOCardDeviceDriver
    {
        private List<IIOCardDevice> m_devices;
        private static IOCardDeviceDriver m_DriverInstance;

        private IOCardDeviceDriver()
        {
#if MUTEX
            bool nResult;
            long VersionNumber = 0;
            m_devices = new List<IIOCardDevice>();

       
            //IO卡接口对象
            IIOCardDevice iOCardDevice = null;

<<<<<<< HEAD

            //for (int i = 0; i < 8; i++) //默认最多8张IO卡
            //{
            //    //判断互斥体，当前IO是否被当前进程或其他进程使用
            //    Mutex mutex = new Mutex(true, "P32" + i, out bool createdNew);
            //    if (createdNew)
            //    {
            //        //遍历PCard32
            //        iOCardDevice = new IOCardP32Device();
            //        nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);
            //        if (!nResult)
            //        {
            //            //板卡没有找到，释放互斥体
            //            if (mutex != null)
            //            {
            //                mutex.ReleaseMutex();
            //                mutex.Dispose();
            //                mutex = null;
            //            }
            //            continue;
            //        }

            //        nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
            //        if (!nResult)
            //        {
            //            //未找到
            //            VersionNumber = -1;
            //        }
            //        iOCardDevice.nCurCardID = i;
            //        iOCardDevice.TypeName = "P32";

            //        iOCardDevice.SerialNumber = iOCardDevice.TypeName + "_" + iOCardDevice.nCurCardID.ToString();


            //        iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

            //        m_devices.Add(iOCardDevice);

            //        //判断后创建完成，释放互斥体
            //        if (mutex != null)
            //        {
            //            mutex.ReleaseMutex();
            //            mutex.Dispose();
            //            mutex = null;
            //        }
            //    }
            //}
=======
         
            for (int i = 0; i < 8; i++) //默认最多8张IO卡
            {
                //判断互斥体，当前IO是否被当前进程或其他进程使用
                Mutex mutex = new Mutex(true, "P32" + i, out bool createdNew);
                if (createdNew)
                {
                    //遍历PCard32
                    iOCardDevice = new IOCardP32Device();
                    nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);
                    if (!nResult)
                    {
                        //板卡没有找到，释放互斥体
                        if (mutex != null)
                        {
                            mutex.ReleaseMutex();
                            mutex.Dispose();
                            mutex = null;
                        }
                        continue;
                    }

                    nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
                    if (!nResult)
                    {
                        //未找到
                        VersionNumber = -1;
                    }
                    iOCardDevice.nCurCardID = i;
                    iOCardDevice.TypeName = "P32";

                    iOCardDevice.SerialNumber = iOCardDevice.TypeName + "_" + iOCardDevice.nCurCardID.ToString();


                    iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

                    m_devices.Add(iOCardDevice);

                    //判断后创建完成，释放互斥体
                    if (mutex != null)
                    {
                        mutex.ReleaseMutex();
                        mutex.Dispose();
                        mutex = null;
                    }
                }
            }
>>>>>>> a6f17ec6bfd703893b0484c6e48577a828427542

            for (int i = 0; i < 8; i++) //默认最多8张IO卡
            {
                //判断互斥体，当前IO是否被当前进程或其他进程使用
<<<<<<< HEAD
                Mutex mutex = new Mutex(true, "P32" + i, out bool createdNew);
=======
                Mutex mutex = new Mutex(true, "E32" + i, out bool createdNew);
>>>>>>> a6f17ec6bfd703893b0484c6e48577a828427542
                if (createdNew)
                {
                    //遍历ECard32
                    iOCardDevice = new IOCardE32Device();
                    nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);
                    if (!nResult)
                    {
                        //板卡没有找到，释放互斥体
                        if (mutex != null)
                        {
                            mutex.ReleaseMutex();
                            mutex.Dispose();
                            mutex = null;
                        }
                        continue;
                    }

                    nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
                    if (!nResult)
                    {
                        //未找到
                        VersionNumber = -1;
                    }

                    iOCardDevice.nCurCardID = i;
<<<<<<< HEAD
                    iOCardDevice.TypeName = "P32";
=======
                    iOCardDevice.TypeName = "E32";
>>>>>>> a6f17ec6bfd703893b0484c6e48577a828427542

                    iOCardDevice.SerialNumber = iOCardDevice.TypeName + "_" + iOCardDevice.nCurCardID.ToString();


                    iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

                    m_devices.Add(iOCardDevice);

                    //判断后创建完成，释放互斥体
                    if (mutex != null)
                    {
                        mutex.ReleaseMutex();
                        mutex.Dispose();
                        mutex = null;
                    }
                }
            }

            #region PIOCard16
            //for (int i = 0; i < 8; i++) //默认最多8张IO卡
            //{
            //    //搜索IO卡时，创建互斥体用来判断，当前IO是否被当前进程或其他进程使用
            //    Mutex mutex = new Mutex(true, "PIOCard16_" + i, out bool createdNew);
            //    if (createdNew)   //未被占用
            //    {
            //        //遍历Card16
            //        iOCardDevice = new IOCardP16Device();
            //        nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);

            //        if (!nResult)
            //        {
            //            //板卡没有找到，释放互斥体
            //            if (mutex != null)
            //            {
            //                mutex.ReleaseMutex();
            //                mutex.Dispose();
            //                mutex = null;
            //            }
            //            continue;
            //        }
            //        nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
            //        if (!nResult)
            //        {
            //            //未找到
            //            VersionNumber = -1;
            //        }
            //        iOCardDevice.nCurCardID = i;
            //        iOCardDevice.SerialNumber = i.ToString();
            //        iOCardDevice.DisplayName = "PIOCard16_";
            //        iOCardDevice.TypeName = iOCardDevice.DisplayName + VersionNumber.ToString();
            //        iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

            //        m_devices.Add(iOCardDevice);
            //        //判断后创建完成，释放互斥体
            //        if (mutex != null)
            //        {
            //            mutex.ReleaseMutex();
            //            mutex.Dispose();
            //            mutex = null;
            //        }
            //    }
            //}
            #endregion


<<<<<<< HEAD
            //for (int i = 0; i < 8; i++) //默认最多8张IO卡
            //{
            //    //搜索IO卡时，创建互斥体用来判断，当前IO是否被当前进程或其他进程使用
            //    Mutex mutex = new Mutex(true, "E16" + i, out bool createdNew);
            //    if (createdNew)   //未被占用
            //    {
            //        //遍历Card16
            //        iOCardDevice = new IOCardE16Device();
            //        nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);

            //        if (!nResult)
            //        {
            //            //板卡没有找到，释放互斥体
            //            if (mutex != null)
            //            {
            //                mutex.ReleaseMutex();
            //                mutex.Dispose();
            //                mutex = null;
            //            }
            //            continue;
            //        }
            //        nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
            //        if (!nResult)
            //        {
            //            //未找到
            //            VersionNumber = -1;
            //        }
            //        iOCardDevice.nCurCardID = i;
            //        iOCardDevice.TypeName = "E16";

            //        iOCardDevice.SerialNumber = iOCardDevice.TypeName + "_" + iOCardDevice.nCurCardID.ToString();


            //        iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

            //        m_devices.Add(iOCardDevice);
            //        //判断后创建完成，释放互斥体
            //        if (mutex != null)
            //        {
            //            mutex.ReleaseMutex();
            //            mutex.Dispose();
            //            mutex = null;
            //        }
            //    }
            //}


=======
            for (int i = 0; i < 8; i++) //默认最多8张IO卡
            {
                //搜索IO卡时，创建互斥体用来判断，当前IO是否被当前进程或其他进程使用
                Mutex mutex = new Mutex(true, "E16" + i, out bool createdNew);
                if (createdNew)   //未被占用
                {
                    //遍历Card16
                    iOCardDevice = new IOCardE16Device();
                    nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);

                    if (!nResult)
                    {
                        //板卡没有找到，释放互斥体
                        if (mutex != null)
                        {
                            mutex.ReleaseMutex();
                            mutex.Dispose();
                            mutex = null;
                        }
                        continue;
                    }
                    nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
                    if (!nResult)
                    {
                        //未找到
                        VersionNumber = -1;
                    }
                    iOCardDevice.nCurCardID = i;
                    iOCardDevice.TypeName = "E16";

                    iOCardDevice.SerialNumber = iOCardDevice.TypeName + "_" + iOCardDevice.nCurCardID.ToString();


                    iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

                    m_devices.Add(iOCardDevice);
                    //判断后创建完成，释放互斥体
                    if (mutex != null)
                    {
                        mutex.ReleaseMutex();
                        mutex.Dispose();
                        mutex = null;
                    }
                }
            }

         
>>>>>>> a6f17ec6bfd703893b0484c6e48577a828427542
#else
            bool nResult;
            long VersionNumber = 0;
            m_devices = new List<IIOCardDevice>();

            //IO卡接口对象
            IIOCardDevice iOCardDevice = null;
            for (int i = 0; i < 8; i++) //默认最多8张IO卡
            {

                //遍历Card16
                iOCardDevice = new IOCard16Device();
                nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);

                if (!nResult)
                {
                    continue;
                }
                nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
                if (!nResult)
                {
                    //未找到
                    VersionNumber = -1;
                }
                iOCardDevice.nCurCardID = i;
                iOCardDevice.SerialNumber = i.ToString();
                iOCardDevice.TypeName = "IOCard16_" + VersionNumber.ToString();
                iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

                m_devices.Add(iOCardDevice);
            }



            for (int i = 0; i < 8; i++) //默认最多8张IO卡
            {

                //遍历Card32
                iOCardDevice = new IOCard32Device();
                nResult = iOCardDevice.OpenIODevice(i, out int ErrorCode, out string ErrorText);
                if (!nResult)
                {
                    continue;
                }

                nResult = iOCardDevice.GetCardVersion(i, ref VersionNumber, out ErrorCode, out ErrorText);
                if (!nResult)
                {
                    //未找到
                    VersionNumber = -1;
                }
                iOCardDevice.nCurCardID = i;
                iOCardDevice.SerialNumber = i.ToString();
                iOCardDevice.TypeName = "IOCard32_" + VersionNumber.ToString();
                iOCardDevice.CloseIODevice(i, out ErrorCode, out ErrorText);

                m_devices.Add(iOCardDevice);

            }
#endif
    
        }




        static IOCardDeviceDriver()
        {
            // --------------------------------------------
            // Create the driver instance
            // --------------------------------------------
            m_DriverInstance = new IOCardDeviceDriver();
        }


        public IIOCardDevice GetDevice(int i)
        {
            // ------------------------------------
            // Return the requested device
            // ------------------------------------
            return m_devices[i];
        }

        public int GetDeviceCount()
        {
            // ------------------------------------
            // Return the number of devices
            // ------------------------------------
            return m_devices.Count;
        }


        //释放所有板卡对象
        public void Release()
        {
            foreach (IIOCardDevice item in m_devices)
            {
                int cardID = item.nCurCardID;
                item.CloseIODevice(cardID, out _, out _);
            }
            m_devices.Clear();
            m_DriverInstance = null;
        }

        //释放指定板卡对象
        public void Release(int CardID)
        {
            IIOCardDevice iOCardDevice = m_devices.Where(x => x.nCurCardID == CardID).First();
            iOCardDevice.CloseIODevice(CardID, out _, out _);
            m_devices.Remove(iOCardDevice);
        }



        internal static IOCardDeviceDriver CreateDriver()
        {
            if (m_DriverInstance == null)
            {
                m_DriverInstance = new IOCardDeviceDriver();
            }
            return m_DriverInstance;
        }

    }
}
